﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

/// <summary>
/// 动画工具
/// </summary>
/// /// <summary>
/// animation tools, used to translate/scale/rotate to target pos/scale/rotation
/// </summary>
public class ZTransformAnim : AnimBase
{

    private void Awake()
    {

    }

    Vector3 LerpFunc(Vector3 a, Vector3 b, float value)
    {
        return a + (b - a) * value;
    }

    public Coroutine RotateTo(Transform trans, Vector3 targetRotation, float time, Action onEnd,bool localRot = false)
    {
        return PlayAnim(targetRotation, time, LerpFunc,
            (rotation) =>
            {
                if(localRot)
                    trans.localRotation = Quaternion.Euler(rotation);
                else
                    trans.rotation = Quaternion.Euler(rotation);
            },
            () =>
            {
                if (localRot)
                    return trans.localRotation.eulerAngles;
                else
                    return trans.rotation.eulerAngles;
            }
           , onEnd);
    }
    Quaternion RotateLerpFunc(Quaternion a, Quaternion b, float value)
    {
        return Quaternion.Lerp(a, b, value); // a + (b - a) * value;
    }
    public Coroutine RotateTo(Transform trans, Quaternion targetRotation, float time, Action onEnd, bool localRot = false)
    {
        return PlayAnim(targetRotation, time, RotateLerpFunc,
            (rotation) =>
            {
                if(trans)
                {
                    if (localRot)
                        trans.localRotation = rotation;
                    else
                        trans.rotation = rotation;
                    
                }
            },
            () =>
            {
                if(trans == null)
                    return Quaternion.identity;
                
                if (localRot)
                    return trans.localRotation;
                else
                    return trans.rotation;
            }
           , onEnd);
    }
    public Coroutine MoveTo(Transform trans, Vector3 targetPosition, float time, Action onEnd)
    {
        return MoveTo(trans, targetPosition, time, onEnd, false);
    }
    public Coroutine MoveTo(Transform trans, Vector3 targetPosition, float time, Action onEnd,bool localPos = false)
    {
        return PlayAnim(targetPosition, time, LerpFunc,
            (pos) =>
            {
                if (float.IsNaN(pos.x) || float.IsNaN(pos.y) || float.IsNaN(pos.z))
                    return;

                if (trans)
                {
                    if (localPos)
                        trans.localPosition = pos;
                    else
                        trans.position = pos;
                }
            },
            () =>
            {
                if (localPos)
                    return trans.localPosition;
                else
                    return trans.position;
            }
           , onEnd);
    }
    
    public Coroutine ScaleTo(Transform trans,Vector3 targetScale,float time, Action onEnd)
    {
        return PlayAnim(targetScale, time, LerpFunc,
            (scale) =>
            {
                if(trans)
                    trans.localScale = scale;
            },
            () =>
            {
                if (trans == null)
                    return Vector3.one;

                return trans.localScale;
            }
           , onEnd);
    }

}
